package com.loongtech.bi.action.bireport;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.report.CountryServerReportManager;
import com.loongtech.bi.manager.report.CountryServerReportManager.GameReportData;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * Author: yuri
 * Description: 运营报表-国家区服
 * Date: 11:52 2017/6/26
 */
@RestController
@Scope("prototype")
@RequestMapping("/bi/countryReport")
public class CountryServerReportAction {
    @Resource
    private CountryServerReportManager countryServerReportManager;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private Session session;

    @RequestMapping(value = "dropdownlist.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> initParam() {
        Map<String, String> serverItmes;
        try {
            // 区服
            serverItmes = new LinkedHashMap<>();
            serverItmes.put("全区服", "0");
            for (EntityBiServer s : BiPlatformManager.SERVER_LIST) {
                serverItmes.put(s.getName(), s.getId());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), serverItmes);
    }

    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> search(@RequestParam String startdate, @RequestParam String enddate, @RequestParam String serverid) {
        sysLogManager.addLog(session.getUserName(), "查询运营报表-区服国家");
        Date dateStart;
        Date dateEnd;
        if (startdate == null) {
            dateStart = new Date(Time.getBeginningOfDay(Time.currentTime()));
        } else {
            dateStart = Time.formatDate(startdate, "yyyy-MM-dd");
        }
        if (enddate == null) {
            dateEnd = new Date(Time.getEndOfDay(Time.currentTime()));
        } else {
            dateEnd = Time.formatDate(enddate, "yyyy-MM-dd");
        }
        List<GameReportData> countryDataList = new ArrayList<>();
        try {
            List<GameReportData> allData = countryServerReportManager.getCountryServerReportData(serverid, dateStart, dateEnd);
            if (allData != null && allData.size() > 0) {
                countryDataList = countryServerReportManager.updateData(allData);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), countryDataList);
    }
}
